<template>
    <div class="enquiry-quotation">
        <van-cell-group>
            <van-cell title="报价生效时间">
                <p class="fix-time-style">
                    <span @click="setTimeSection = 'start'">{{ timeSection.start }}</span> 至 <span
                    @click="setTimeSection = 'end'">{{ timeSection.end }}</span>
                </p>
            </van-cell>
            <van-cell title="报价生效地区" :value="region.name" is-link @click="region.show = true"/>
        </van-cell-group>
        <div v-show="pga > 0" @click="eventGoBackOperate" class="goback-button">返回上一级</div>
        <van-list v-model="dataset.loading" :finished="dataset.finished" finished-text="没有更多了"
                  @load="controllerRegionQuotedPriceList" class="list-contianer">
            <dl v-for="(item, index) in dataset.items" :key="index" class="offer-list-wrap">
                <dt class="title">
                    <div>{{ item.name }}</div>
                    <div>
                        <p v-for="(itema, indexa) in item.oda.danwei" :key="indexa">
                            <span>￥{{ itema.price }}</span><span>/{{ itema.danweiming }}</span></p>
                    </div>
                </dt>
                <dd class="button">
                    <p>
                        <img src="../../../static/images/folder.png"/>
                        <span @click="eventClickEntryNextClassify(index)">进入下级</span>
                    </p>
                    <!--按钮移到外层-->
                    <p v-show="pga <= 0">
                        <img src="../../../static/images/edit-orange.png"/>
                        <span @click="eventClickEditQuotedPrice(index)">编辑价格</span>
                    </p>
                    <!--内层按钮不动-->
                    <p v-show="pga > 0">
                        <img src="../../../static/images/edit-orange.png"/>
                        <span @click="eventClickEditQuotedPrice(index)">编辑价格</span>
                    </p>
                </dd>
            </dl>
        </van-list>
        <van-popup v-model="timeSection.show" position="bottom">
            <van-datetime-picker v-model="timeSection.value" @cancel="timeSection.show = false"
                                 @confirm="setTimeSection = 'set'" type="datetime"/>
        </van-popup>
        <van-popup v-model="region.show" position="bottom">
            <!-- <van-picker show-toolbar :columns="region.areaList" @cancel="region.show = false" @confirm="eventClickSelectOperate" /> -->
            <van-area :value="region.code" :area-list="region.areaList" @cancel="region.show = false"
                      @confirm="eventClickSelectOperate"/>
        </van-popup>
    </div>
</template>

<script>
    import {Cell, CellGroup, List, Popup, DatetimePicker, Picker, Area} from 'vant';

    import AreaList from '../../libs/js/area.js';

    export default {
        name: 'AccountingQuotedPriceManage',
        components: {
            [List.name]: List,
            [Cell.name]: Cell, [CellGroup.name]: CellGroup,
            [Popup.name]: Popup, [DatetimePicker.name]: DatetimePicker, [Picker.name]: Picker,
            [Area.name]: Area
        },
        data() {
            return {
                pga: 0, pgaList: [], setTimeSection: '',
                dataset: {
                    index: '',
                    page: 0, limit: 30, total: 0,
                    loading: false, finished: true,
                    items: []
                },
                timeSection: {
                    show: false, value: new Date(),
                    start: this.$moment().startOf('month').format('MM-DD HH:mm'),
                    ostart: this.$moment().startOf('month').format('YYYY-MM-DD HH:mm'),
                    end: this.$moment().endOf('month').format('MM-DD HH:mm'),
                    oend: this.$moment().endOf('month').format('YYYY-MM-DD HH:mm')
                },
                region: {
                    show: false,
                    name: '石家庄-长安区', code: '130102',
                    areaList: []
                }
            }
        },
        mounted() {
            // console.log(this.$route.query);
            if (this.$route.query.region) {
                this.region.code = this.$route.query.region;
                this.region.name = this.$route.query.name;
            }

            // this.reqeustRegionList();
            this.region.areaList = AreaList;
            this.controllerRegionQuotedPriceList();
        },
        methods: {

            eventGoBackOperate() {
                var pga = this.pgaList.pop();
                if (pga) {
                    if (this.pga == pga) {
                        this.eventGoBackOperate();
                        return;
                    } else {
                        this.pga = pga;
                    }
                } else {
                    this.pga = 0;
                }

                this.dataset.page = 0;
                this.dataset.items = [];
                this.dataset.finished = false;
            },
            eventClickSelectOperate(params) {
                // console.log(params);
                if (params[2]) {
                    this.region.name = params[1].name + '-' + params[2].name;
                    this.region.code = params[2].code;
                } else {
                    this.region.name = '石家庄-长安区';
                    this.region.code = '130102';
                }

                this.dataset.page = 0;
                this.dataset.items = [];
                this.dataset.finished = false;

                this.region.show = false;

                // this.region.name = params.text;
                // this.region.code = params.value;

                // this.controllerRegionQuotedPriceList();
            },
            eventClickEditQuotedPrice(index) {
                this.dataset.index = index;

                var item = this.dataset.items[index],
                    params = {
                        item, region: {
                            name: this.region.name,
                            code: this.region.code
                        },
                        areaList: this.region.areaList
                    };

                this.$emit('eventOnTransmit', params);
                // console.log(item);
                // this.$router.push({
                //     name: 'AccountingQuotedPriceEdits', params :
                // });
            },
            reqeustRegionList() {
                var url = '/api/admin/regiongroup/getlist',
                    params = {
                        page: 0, list_rows: 100
                    };

                this.$util.postRequestInterface(url, params, (res) => {
                    for (let x of res.data.data) {
                        this.region.areaList.push({
                            text: x.name, value: x.id
                        });
                    }
                    // console.log(this.region.areaList);
                    if (this.region.areaList.length > 0) {
                        this.region.name = this.region.areaList[0].text;
                        this.region.code = this.region.areaList[0].value;
                        this.controllerRegionQuotedPriceList();
                    }
                });
            },
            controllerRegionQuotedPriceList() {
                var url = '/api/home/garbage/getlist',
                    params = {
                        page: this.dataset.page,
                        list_rows: this.dataset.limit,
                        regionz: this.region.code,
                        start_time: this.timeSection.ostart,
                        end_time: this.timeSection.oend,
                        pga: this.pga || 0,
                        status: 1
                    };

                this.$util.postRequestInterface(url, params, (res) => {

                    for (let x of res.data.data) {
                        let oData = x.danwei.filter(val => val.danweiming == "kg")[0];
                        console.log('logs ', oData)
                        let danwei = x.danwei.map(val => {
                            if (val.danweiming.toLowerCase == "kg") {
                                return val
                            }

                            return Object.assign(val, {
                                price: parseFloat(oData.price) * parseFloat(val.transweight)
                            })

                        });
                        window.localStorage.setItem("danwei",JSON.stringify(danwei))
                        this.dataset.items.push({
                            id: x.id,
                            gid: x.garbageid,
                            name: x.name,
                            pga: x.pga,
                            pgalist: x.pgalist,
                            token: x.token,
                            oda: Object.assign(x, {
                                danwei: danwei
                            })
                        });
                    }

                    this.dataset.page++;
                    if (this.dataset.items.length >= res.data.count) this.dataset.finished = true;
                    this.dataset.loading = false;
                });

            },
            eventClickEntryNextClassify(index) {
                var item = this.dataset.items[index];

                if (this.pgaList.indexOf(item.id) < 0) this.pgaList.push(item.id);

                this.dataset.page = 0;
                this.dataset.items = [];
                this.dataset.finished = false;
                this.dataset.loading = false;
                this.pga = item.id;
            },
            reloadFun(params) {
                var index = this.dataset.index;
                if (params) {
                    // console.log('logs ', this.dataset.items[index].oda.danwei)
                    let danwei = this.dataset.items[index].oda.danwei;

                    let oData = danwei.filter(val => val.danweiming == "kg")[0];
                    oData.price = params.price;
                    console.log('logs ', oData)
                    this.dataset.items[index].oda.danwei = danwei.map(val => {
                        if (val.danweiming.toLowerCase == "kg") {
                            return val
                        }

                        return Object.assign(val, {
                            price: parseFloat(oData.price) * parseFloat(val.transweight)
                        })

                    });


                }
                // console.log('quoted-price-manage', this.dataset.items[index], params);
                /*   this.dataset.page = 0;
                   this.controllerRegionQuotedPriceList();*/
            }
        },
        watch: {
            setTimeSection(nVal, oVal) {
                // console.log(nVal, oVal);

                if (nVal == 'start' || nVal == 'end') {
                    this.timeSection.show = true;
                    this.timeSection.value = (nVal == 'start') ? new Date(this.timeSection.ostart) : new Date(this.timeSection.oend);
                    this.setTimeSection = 'is-' + nVal;
                }

                if (nVal == 'set' && oVal == 'is-start') {
                    this.timeSection.start = this.$moment(this.timeSection.value).format('MM-DD HH:mm');
                    this.timeSection.ostart = this.$moment(this.timeSection.value).format('YYYY-MM-DD HH:mm');
                    this.timeSection.show = false;
                } else if (nVal == 'set' && oVal == 'is-end') {
                    this.timeSection.end = this.$moment(this.timeSection.value).format('MM-DD HH:mm');
                    this.timeSection.oend = this.$moment(this.timeSection.value).format('YYYY-MM-DD HH:mm');
                    this.timeSection.show = false;
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .page-top {
        position: relative;
        padding: 0rem 1rem;
        border-bottom: 0.4rem solid #f4f4f4;
    }

    .quoted-price-time {
        padding: 1rem 0rem;
        border-bottom: 0.1rem solid #f4f4f4;
    }

    .offer-list-wrap {
        padding: 0rem 1rem;
        border-bottom: 0.4rem solid #f4f4f4;

        .title {
            padding: 1.3rem 0rem 1rem;
            display: flex;
            border-bottom: 0.1rem solid #f4f4f4;

            p {
                font-size: 1.4rem;
                text-align: right;
                padding-bottom: 0.5rem;

                span:first-child {
                    color: #ff0000;
                }
            }

            div:first-child {
                padding-right: 1rem;
            }

            div:last-child {
                flex: 1;
            }
        }

        .button {
            text-align: right;
            font-size: 1.3rem;
            padding: 1.3rem 0rem;
            display: flex;
            flex-direction: row-reverse;

            p {
                &:first-child {
                    margin-left: 1.6rem;
                }

                img {
                    width: 1.7rem;
                    vertical-align: middle;
                    margin-right: 0.5rem;
                }

                span {
                    position: relative;
                    top: 1.5px;
                }
            }
        }
    }

    .goback-button {
        background-color: #00cc00;
        padding: 1rem;
        color: #ffffff;
        margin-bottom: 1rem;
    }

    .van-cell__title {
        flex: none;
    }

    .van-cell__value {
        flex: 1;
    }

    .fix-time-style {
        text-align: right;
    }

    .list-contianer {
        padding-bottom: 6rem;
    }
</style>
